package com.example.james.providertest;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    String newId = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button addData = findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.example.james.litepaltest.provider/book");
                ContentValues contentValues = new ContentValues();
                contentValues.put("name", "A Clash of Kings");
                contentValues.put("author", "George Martin");
                contentValues.put("pages", 1040);
                contentValues.put("price", 22.85);
                Uri newUri = getContentResolver().insert(uri, contentValues);
                newId = newUri.getPathSegments().get(1);
                Toast.makeText(MainActivity.this, "New Id: " + newUri.getPathSegments().get(1), Toast.LENGTH_SHORT).show();
            }
        });
        Button queryData = findViewById(R.id.query_data);
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.example.james.litepaltest.provider/book");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if (cursor != null){
                    StringBuilder stringBuilder = new StringBuilder();
                    while (cursor.moveToNext()){
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        String pages = cursor.getString(cursor.getColumnIndex("pages"));
                        String price = cursor.getString(cursor.getColumnIndex("price"));
                        stringBuilder.append(String.format("Name: %s; Author: %s; Pages: %s; Price: %s", name, author, pages, price));
                    }
                    Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
                    cursor.close();
                }
            }
        });
        Button updateData = findViewById(R.id.update_data);
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.example.james.litepaltest.provider/book/" + newId);
                ContentValues contentValues = new ContentValues();
                contentValues.put("price", 100);
                contentValues.put("pages", 999);
                int updateRows = getContentResolver().update(uri, contentValues, null, null);
                Toast.makeText(MainActivity.this, "Update Rows: " + updateRows, Toast.LENGTH_SHORT).show();
            }
        });

        final Button deleteData = findViewById(R.id.delete_data);
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.example.james.litepaltest.provider/book/" + newId);
                int deleteRows = getContentResolver().delete(uri, null, null);
                Toast.makeText(MainActivity.this, "Delete Rows: " + deleteRows, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
